from marshmallow import Schema, fields, validate, validates, ValidationError
from datetime import datetime

class InvoiceItemSchema(Schema):
    """发票项目模式"""
    description = fields.Str(required=True)
    quantity = fields.Int(required=True)
    unitPrice = fields.Float(required=True)
    amount = fields.Float(required=True)

class InvoiceSchema(Schema):
    """发票模式"""
    invoiceId = fields.Str()
    invoiceNumber = fields.Str()
    paymentId = fields.Str(required=True)
    billingId = fields.Str(required=True)
    patientId = fields.Str(required=True)
    patientName = fields.Str(required=True)
    items = fields.List(fields.Nested(InvoiceItemSchema), required=True)
    totalAmount = fields.Float(required=True)
    paidAmount = fields.Float(required=True)
    insurancePaid = fields.Float(dump_default=0)
    invoiceDate = fields.DateTime(dump_default=datetime.utcnow)
    invoiceUrl = fields.Str()
    status = fields.Str(validate=validate.OneOf(['待开具', '已开具', '已作废']), dump_default='待开具')
    createdAt = fields.DateTime(dump_default=datetime.utcnow)
    updatedAt = fields.DateTime(dump_default=datetime.utcnow)

class InvoiceGenerateSchema(Schema):
    """生成发票模式"""
    paymentId = fields.Str(required=True)
    
class InvoiceQuerySchema(Schema):
    """发票查询模式"""
    patientId = fields.Str()
    paymentId = fields.Str()
    billingId = fields.Str()
    status = fields.Str(validate=validate.OneOf(['待开具', '已开具', '已作废', 'all']))
    startDate = fields.Date()
    endDate = fields.Date() 